﻿/*
Copyright (c) 2008 Ryan BB Schreiber, Brendan G Fry

Permission is hereby granted, free of charge, to any person obtaining a copy of 
this software and associated documentation files (the "Software"), to deal in 
the Software without restriction, including without limitation the rights to 
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 
of the Software, and to permit persons to whom the Software is furnished to do 
so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all 
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE 
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 
*/
using System.Diagnostics;
using Castle.ActiveRecord;
using NHibernate.Criterion;

namespace Darkside.GeneratorStudio.Library.Domain
{
    public class ReferencedTableInfo
    {

        private readonly Table _Table;
        private readonly Column _Column;
        private readonly Table _ReferencedTable;
        private readonly Column _ReferencedColumn;
 
        protected ReferencedTableInfo(){}

        public ReferencedTableInfo(Table table, Table ReferencedTable, string columnName, string ReferencedColumnName)
        {
            _Table = table;
            _ReferencedTable = ReferencedTable;
            using (new SessionScope(FlushAction.Never))
            {
                _Column = ActiveRecordBase<Column>.FindFirst(new ICriterion[] {Restrictions.Eq("Name", columnName), Restrictions.Eq("TableName", table.Name)});
                _ReferencedColumn =
                    ActiveRecordBase<Column>.FindFirst(new ICriterion[] {Restrictions.Eq("Name", ReferencedColumnName), Restrictions.Eq("TableName", ReferencedTable.Name)});
                Debug.Assert(_ReferencedColumn!=null);
            }
        }
        public Table Table { get { return (_Table); } }
        public Column Column { get { return (_Column); } }
        public Table ReferencedTable { get { return (_ReferencedTable); } }
        public Column ReferencedColumn { get { return (_ReferencedColumn); } }


        public string DescriptiveName
        {
            get
            {
                return (Table.Name + " - [" + Table.Name + "." + Column.Name + " = " + ReferencedTable.Name + "." + ReferencedColumn.Name + "]");
            }
        }

        public override string ToString()
        {
            return (DescriptiveName);
        }

        public bool IsActive { get; set; } 

    }
}
